import {body, description, request, summary, tagsAll} from "koa-swagger-decorator-fork";
import searcher from "../../utils/search";

const SearchEsSchema = {
    from: {type: 'number',default:0},
    size: {type: 'number',default:1000},
    body:{type:'object'},
    keyword:{type:'string'}
}

@tagsAll(['Concept'])
export default class ConceptRouter {
    @request('POST', '/search/concepts')
    @summary('search concepts')
    @body(SearchEsSchema)
    @description('搜索概念')
    static async searchConcept(ctx) {
        let result = await searcher.retrieve(ctx.request.body)
        ctx.body = result
    }
}

export const loadGraphConcept = async (data)=>{
    let nodes = []
    for(let model of data.mapModels){
        nodes = nodes.concat(model.nodes.map((node)=>{
            node.graphid = model.id
            node.title = data.title
            return node
        }))
    }
    await searcher.batchSave(nodes)
}
